<template>
  <div>
    <div style="margin-bottom: 10px;width: 800px"  class="animate__animated animate__zoomIn">
      <el-card class="box-card">
        <div slot="header" class="clearfix"><span style="font-size: 24px">设置教师打分时间</span></div>
        <div class="block" style="display: inline-block;margin-right: 5px">
          <el-date-picker
              v-model="param1.start"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择开始时间">
          </el-date-picker>
        </div>
        至
        <div class="block" style="display: inline-block;margin-left: 5px">
          <el-date-picker
              v-model="param1.end"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择结束时间">
          </el-date-picker>
        </div>
        <el-switch active-text="开启打分" inactive-text="关闭打分" v-model="param1.status === '1' ? true : false"
                   @change="statusChangeOfTeacher"
                   style="margin-left: 20px"></el-switch>
      </el-card>
    </div>

    <div style="margin-bottom: 10px;width: 800px" class="animate__animated animate__zoomIn">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span style="font-size: 24px">设置学生选课时间</span>
        </div>
        <div class="block" style="display: inline-block;margin-right: 5px">
          <el-date-picker
              v-model="param2.start"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择开始时间">
          </el-date-picker>
        </div>
        至
        <div class="block" style="display: inline-block;margin-left: 5px">
          <el-date-picker
              v-model="param2.end"
              type="datetime"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择结束时间">
          </el-date-picker>
        </div>
        <el-switch active-text="开启选课" inactive-text="关闭选课" v-model="param2.status === '1' ? true : false"
                   @change="statusChangeOfStudent"
                   style="margin-left: 20px"></el-switch>
        <br><br>
        请输入学生可以选课程的数量(默认为0)：
        <template>
          <el-input-number v-model="param2.count" :min="1"></el-input-number>
        </template>
      </el-card>
    </div>
  </div>
</template>

<script>
import request1 from "@/utils/request1";

export default {
  name: "TimeSet",
  data() {
    return {
      param1: {
        usertype: 'teacher',
        start: '',
        end: '',
        status: '',
      },
      param2: {
        usertype: 'student',
        start: '',
        end: '',
        status: '',
        count: '0'
      }
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      request1.get('/timeSet/getTeacher').then(res => {
        if (res.data != null) {
          request1.get('/timeSet/getTeacher').then(res => {
            this.param1 = res.data
          })
        } else {
          this.param1 = {usertype: 'teacher'}
        }
      })
      request1.get('/timeSet/getStudent').then(res => {
        if (res.data != null) {
          request1.get('/timeSet/getStudent').then(res => {
            this.param2 = res.data
          })
        } else {
          this.param2 = {usertype: 'student'}
        }
      })
    },
    statusChangeOfTeacher() {
      if (Date.parse(this.param1.start) > Date.parse(this.param1.end)) {
        this.$notify.error('开始时间不能大于结束时间')
      } else {
        if (this.param1.status === '1') {
          this.param1.status = '0'
          this.$notify.success('打分时间已关闭')
        } else {
          this.param1.status = '1'
          this.$notify.success('打分时间已开启')
        }
        request1.get('/timeSet/getTeacher').then(res => {
          if (res.data === null) {
            request1.post('/timeSet/insertInfo', this.param1).then(res => {
            })
          } else {
            request1.put('/timeSet/update', this.param1).then(res => {
            })
          }
        })
      }
    },
    statusChangeOfStudent() {
      if (Date.parse(this.param2.start) > Date.parse(this.param2.end)) {
        this.$notify.error('开始时间不能大于结束时间')
      } else {
        if (this.param2.status === '1') {
          this.param2.status = '0'
          this.$notify.success('选课时间已关闭')
        } else {
          this.param2.status = '1'
          this.$notify.success('选课时间已开启')
        }
        request1.get('/timeSet/getStudent').then(res => {
          if (res.data === null) {
            console.log(this.param2)
            request1.post('/timeSet/insertInfo', this.param2).then(res => {
            })
          } else {
            request1.put('/timeSet/update', this.param2).then(res => {
            })
          }
        })
      }
    }
  }
}
</script>
